﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Grinder.Renamer;
using Grinder.Properties;

namespace Grinder.Forms
{
    public sealed partial class FindReplaceForm : Form
    {
        private readonly ImageList _imageList;
        
        public IEnumerable<FindReplaceItem> FindReplaceItems
        {
            get
            {
                var frItems = new List<FindReplaceItem>();

                foreach (ListViewItem item in listFindReplace.Items)
                {
                    var frItem = item.Tag as FindReplaceItem;
                    if (frItem != null)
                        frItems.Add(frItem);
                }
                return frItems;
            }
        }
        
        public FindReplaceForm()
        {
            InitializeComponent();

            _imageList = new ImageList {ColorDepth = ColorDepth.Depth32Bit};
            _imageList.Images.Add(Resources.text_replace_disabled);

            listFindReplace.SmallImageList = _imageList;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textFind.Text)) return;
            
            var item = listFindReplace.Items.Add("",0);
            item.SubItems.Add(textFind.Text);
            item.SubItems.Add(textReplace.Text);
            item.Tag = new FindReplaceItem(textFind.Text,
                                           textReplace.Text,
                                           checkRegexp.Checked);

            textFind.Text = textReplace.Text = "";
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listFindReplace.SelectedItems)
            {
                listFindReplace.Items.Remove(item);
            }
        }

        private void btnMoveUp_Click(object sender, EventArgs e)
        {
            listFindReplace.MoveSelectedUp();
        }

        private void btnMoveDown_Click(object sender, EventArgs e)
        {
            listFindReplace.MoveSelectedDown();
        }
        
    }
}
